package MyCircularQueue;

public class MyCircularQueue<T> implements IQueue<T>{
    private T[] elem;
    private int front;
    private int rear;

    public MyCircularQueue(int k) {
        this.elem = (T[]) new Object[k+1];
    }

    @Override
    public boolean offer(T val) {
        if(isFull())
        {
            return false;
        }
        elem[rear] = val;
        rear = (rear + 1) % elem.length;
        return true;
    }

    @Override
    public T poll() {
        if(empty()) return null;

        T ret = elem[front];
        front = (front + 1) % elem.length;
        return ret;
    }

    @Override
    public T peek() {
        if(empty())
        {
            return null;
        }
        return elem[front];
    }

    @Override
    public boolean empty() {
        return front == rear;
    }

    @Override
    public int size() {
        return (rear + elem.length - front) % elem.length;
    }

    public boolean isFull()
    {
        return (rear + 1) % elem.length == front;
    }
}
